package com.snow.useviewdemo.function.audio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.snow.common.base.BaseBasesActivity;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.playl.interfaze.MusicPlayStateListenerHelper;
import com.snow.playl.manager.PlayAudioManager;
import com.snow.useviewdemo.APPApplication;
import com.snow.useviewdemo.R;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.autosize.utils.LogUtils;

import static android.media.AudioManager.ADJUST_RAISE;
import static android.media.AudioManager.FLAG_SHOW_UI;
import static android.media.AudioManager.STREAM_MUSIC;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-11-07
 * desc   :文字转语音  初始化失败
 */
public class WordChangeAudioActivity extends BaseBasesActivity {
    private final String TAG = "snow";
    private static TextToSpeech tts;
    private String chinaMsg = "董明珠向雷军发起新赌约：让中国装备叫响世界请雷军提出自己的目标";
    private String engMsg = "Dong Mingzhu launched a new bet on Lei Jun: let China's equipment make the world famous and ask Lei Jun to put forward its own goals";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_word_change_audio;
    }

    @Override
    protected void initView() {
        init();
    }

    @OnClick({R.id.btn_00, R.id.btn_01, R.id.btn_02})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_00:
                init();
                break;
            case R.id.btn_01:
                tts.speak(chinaMsg, TextToSpeech.QUEUE_FLUSH, null);
                break;
            case R.id.btn_02:
                tts.speak(engMsg, TextToSpeech.QUEUE_FLUSH, null);
                break;
        }
    }

    private void init() {
        //初始化tts监听对象
        tts = new TextToSpeech(APPApplication.getInstance(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                // 判断是否转化成功
                if (status == TextToSpeech.SUCCESS) {
                    LogUtils.e("初始化===成功");
                    //默认设定语言为中文，原生的android貌似不支持中文。
                    int result = tts.setLanguage(Locale.CHINESE);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        ToastUtils.getInstance().showToast("初始化完成");
                        LogUtils.e("初始化===支持此语音");
                    } else {
                        LogUtils.e("初始化===不===支持此语音");
                        //不支持中文就将语言设置为英文
                        tts.setLanguage(Locale.US);
                    }
                } else {
                    ToastUtils.getInstance().showToast("初始化===失败");
                    LogUtils.e("初始化===失败");
                }
            }
        });
        tts.setLanguage(Locale.CHINA);
        tts.setPitch(1.0f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
        //设置语速
        tts.setSpeechRate(0.5f);
    }
}
